﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Globalization;
using Un4seen.Bass;

namespace mxplay
{
    class BooleanLocalizedConvertor : BooleanConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
        {
            return (bool)value ? lang.True : lang.False;
        }

        public override object ConvertFrom(ITypeDescriptorContext context,
          CultureInfo culture,
          object value)
        {
            return (string)value == lang.True;
        }
    }

    class BassDeviceConvertor : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            int dev_count=Bass.BASS_GetDeviceCount();

            List<int> dev_ids=new List<int>();
            dev_ids.Add(-1);    //устройство по умолчанию
            //dev_ids.Add(0);     //нет выхода
            for (int i=0; i < dev_count; i++)
            {
                dev_ids.Add(i);
            }

            return new StandardValuesCollection(dev_ids);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                int dev_id=(int)value;
                switch (dev_id)
                {
                    case -1:
                        return lang.DefaultDevice;
                    //case 0:
                    //    return lang.NoSoundDevice;
                    default:
                        return Bass.BASS_GetDeviceInfo(dev_id).name;
                }
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            string dev_name=value.ToString();
            if (dev_name == lang.DefaultDevice)
            {
                return -1;
            }
            //if (dev_name == lang.NoSoundDevice)
            //{
            //    return 0;
            //}
            BASS_DEVICEINFO[] dev_infos=Bass.BASS_GetDeviceInfos();
            for (int dev_id=0; dev_id < dev_infos.Length; dev_id++)
            {
                if (dev_infos[dev_id].name == dev_name) return dev_id;
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string)) return true;

            return base.CanConvertTo(context, destinationType);
        }
    }
}
